<?php
namespace app\h5api\controller;


use app\common\model\Notice;
use app\common\model\NoticeUser;
use app\common\model\Service as ServiceModel;
use app\common\model\ServiceMiddle;
use app\common\model\ServiceType;
use think\facade\Config;

class Service extends Base
{

    /**
     * 服务列表
     */
    public function ServiceList()
    {
        $TypeModel = new ServiceType();
        $lon = input('lon','');
        $lat = input('lat','');
        $rs_data = $TypeModel->GetList($lon, $lat);

        $this->result($rs_data,1,'返回成功');

    }

    /**
     * 店铺服务列表
     */
    public function ShopServiceList()
    {
        $ServiceModel = new ServiceModel();
        $page = input('page',1);
        $cond['shop_id'] = input('shop_id');
        $cond['approved'] = 2;
        $cond['status'] = 1;
        $rs_data = $ServiceModel->get_list($cond,'sort asc','service_id,service_name,top_pic,all_num,price,sale_price',$page,$this->limit);
        $this->result($rs_data,1,'返回成功');

    }

    /**
     * 服务详情
     */
    public function ServiceInfo()
    {
        $serviceId = input('service_id');
        if(!$serviceId){
            $this->result('',-1,'请提交正确的服务ID');
        }

        $rs_data = \app\common\model\Service::get($serviceId);
        $image = $rs_data->serviceimg()->where('is_show',1)->order('sort asc')->select();

        $tvar = [
            'service' => $rs_data,
            'image' => $image,
        ];
        $this->result($tvar,1);

    }

	/**
	 * 服务下单
	 */
	public function DownOrder()
	{
		$OrderModel = new \app\common\model\Order();
		$make_time = input('make_time');
		$address_id = input('address_id');
		$service_id = input('service_id');
		$pay_end = input('pay_end', 1);
		$quantity = input('quantity',1);

		$user = $this->GetUserInfo();

        if ($pay_end == 1) {
            $wxconfig = Config::pull('weixin');
        } else {
            $wxconfig = Config::pull('weixin_applet');
        }

		$data = $OrderModel->ServiceOrder($make_time, $address_id, $service_id, $quantity,$user);
		$notify = "/h5/notify";
		$rs = $OrderModel->wxpay($user,$data['pay_id'],$wxconfig,$notify, $pay_end);
		if($rs === false){
			$this->result([],-1,$OrderModel->getError());
		}
		$rs['order_id'] = $data['order_id'];
		$rs['send_id'] = $data['send_id'];
		$this->result($rs,1);
	}

    /**
     * 服务订单列表
     */
    public function OrderList()
    {
        $orderModel = new \app\common\model\Order();
        $page = input('page');
        $is_page = input('is_page');
        $cond['state'] = input('state',1);

        $cond['order_type'] = 3;
        $cond['user_id'] = $this->_userid;
        $field = "a.order_id,a.is_comment,a.order_status,a.pay_status,a.order_status,a.pay_amount,a.pay_time,b.make_time,b.send_id,b.state,b.service_id,b.quantity,c.service_name,c.top_pic";

        $rs_data= $orderModel->get_Service_Order_list($cond,'a.pay_time desc',$field, $page, $this->limit, $is_page);
        $this->result($rs_data,1);
    }

    /**
     * 服务订单详情
     */
    public function OrderServiceInfo()
    {
        $orderModel = new \app\common\model\Order();
        $ServiceModel = new ServiceMiddle();
        $send_id = input('send_id');
        $cond['send_id'] = $send_id;
        $field = "a.order_id,a.is_comment,a.address_id,a.receive_name,a.p_c_a,a.r_address,a.r_phone,a.order_status,a.orderno,a.pay_status,a.order_status,a.pay_amount,a.pay_time,b.make_time,b.send_id,b.state,b.service_id,b.quantity,c.service_name,c.top_pic,d.team_name,d.phone";

        $rs = $orderModel->get_Service_Order_list($cond,'',$field);

        $ServiceModel->where('send_id', $send_id)->update(['is_show' => 2]);
        $this->result($rs,1);
    }

    /**
     * 服务售后 取消订单
     */
    public function ServiceAfter()
    {
        $send_id = input('send_id');
        $ServiceModel = new ServiceModel();
        $rs = $ServiceModel->ServiceAfter($send_id);
        if($rs === false){
            $this->result('',-1,'已超过预约时间，请联系官方客服');
        }
        $this->result('',1,'已申请售后');
    }

    /**
     * 服务完成
     */
    public function Complete()
    {
        $ServiceModel = new ServiceModel();
        $sendId = input('send_id');
        $rs = $ServiceModel->Complete($sendId);
        if($rs === false){
            $this->result('',-1, $ServiceModel->getError());
        }

        $this->result('',1,'操作成功');
    }

    /**
     * 服务评价
     */
    public function iscomment()
    {
        $ServiceModel = new ServiceModel();

        if (input('post.handle') == 'submit') {
            $params = input('post.');

            $rs = $ServiceModel->isComment($params, 9);
            if($rs === false){
                $this->result('',-1, $ServiceModel->getError());
            }
            $this->result('',1,'评价成功');
        }
    }

    /**
     * 消息通知列表
     */
    public function NoticeList()
    {

        $NoticeModel = new Notice();
        $cond['user_id'] = $this->_userid;
        $rs_data = $NoticeModel->get_list_page($cond,'id desc','a.id,a.shop_id,a.user_id,a.title,a.summary,a.add_time,c.shop_name,c.imagesurl', $this->limit);
        $this->result($rs_data,1);
    }


    /**
     * 消息通知列表
     */
    public function NoticeInfo()
    {
        $NoticeModel = new Notice();
        $NoticeUserModel = new NoticeUser();

        $cond['id'] = input('id');
        $rs_data = $NoticeModel->getOne($cond,'id,title,summary,content,add_time');

        $data = $NoticeUserModel->getOne(['user_id' => $this->_userid,'notice_id' => $cond['id']],'id');
        if(!$data){
            $data = [
                'user_id' => $this->_userid,
                'notice_id' => $cond['id'],
            ];
            $rs = $NoticeUserModel->edit($data);
        }
        $this->result($rs_data,1);
    }
}
